package com.kc.sys.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kc.stand.domain.StandFileList;
import com.kc.stand.service.StandFineListService;
import com.kc.sys.common.DataGridView;
import com.kc.sys.common.R;
import com.kc.sys.common.TreeNode;
import com.kc.sys.domain.Station;
import com.kc.sys.service.StationService;
import com.kc.sys.vo.StationVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老杜
 * @since 2019-10-21
 */
@RestController
    @RequestMapping("/station")
public class StationController {

    @Autowired
    private StationService stationService;
    @Autowired
    private StandFineListService standFineListService;

    /**
     *加载车站管理左边的车站树结构Json
     */
    @RequestMapping("loadStationManagerLeftJson")
    public DataGridView loadStationManagerLeftJson(StationVo stationVo){
        List<Station> list = stationService.list();
        List<TreeNode> treeNodes = new ArrayList<>();
        for (Station station : list) {
            Boolean spread = station.getOpen()==1?true:false;
            treeNodes.add(new TreeNode(station.getId(),station.getPid(),station.getTitle(),spread));

        }
        return new DataGridView(treeNodes);
    }

    @RequestMapping("loadAllStation")
    public DataGridView loadAllStation(StationVo stationVo){
        //获取用户session
        //User user = (User)WebUtils.getSession().getAttribute("user");
        IPage<Station> page = new Page<>(stationVo.getPage(),stationVo.getLimit());
        QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(stationVo.getTitle()),"title",stationVo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(stationVo.getAddress()),"address",stationVo.getAddress());
        queryWrapper.like(StringUtils.isNotBlank(stationVo.getRemark()),"remark",stationVo.getRemark());
        queryWrapper.eq(stationVo.getId()!=null,"id",stationVo.getId()).or().eq(stationVo.getId()!=null,"pid",stationVo.getId());
        queryWrapper.orderByAsc("ordernum");
        stationService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    //加载最大的排序码
    @RequestMapping("loadStationMaxOrderNum")
    public Map<String ,Object> loadStationMaxOrderNum(){
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("ordernum");
        List<Station> list = stationService.list(queryWrapper);
        if(list.size()>0){
            map.put("value",list.get(0).getOrdernum()+1);
        }else{
            map.put("value",1);
        }
        return map;
    }

    //添加车站
    @RequestMapping("addStation")
    public R addStation(StationVo stationVo){
        try {
            stationVo.setCreatetime(new Date());
            stationVo.setOpen(1);
            stationVo.setPid(1);
            System.out.println(stationVo.getPid()+"pid");
            stationService.save(stationVo);
            return R.ok().message("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("添加失败");
        }
    }

    //更新车站
    @RequestMapping("updateStation")
    public R updateStation(StationVo stationVo){
        try {
            stationService.updateById(stationVo);
            return R.ok().message("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("更新失败");
        }
    }
    /**
     * 根据Id 删除
     * */
    @RequestMapping("deleteStation")
    public R deleteById(Integer id){
        try {
            stationService.removeById(id);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }

    //检查当前车站有没有子节点（车站）
    @RequestMapping("checkStationHasChildrenNode")
    public Map<String ,Object> checkStationHasChildrenNode(StationVo stationVo){
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",stationVo.getId());
        List<Station> list = stationService.list(queryWrapper);
        if(list.size()>0){
            map.put("value",true);
        }else {
            map.put("value",false);
        }
        return map;
    }
    @RequestMapping("loadAllStationForSelect")
    public DataGridView loadAllStationForSelect(){
        QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id",1);
        List<Station> stations = stationService.list(queryWrapper);
        return new DataGridView(stations);
    }

    @RequestMapping("loadAllStationSelect")
    public DataGridView loadAllStationSelect(Integer stationId){
       /* QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id",1)
                .ne("id",stationId);*/
        List<StandFileList> list = standFineListService.list();
        List<Station> stations = new ArrayList<>();
        for (StandFileList fileList : list) {
            Integer sid = fileList.getSid();
            if(sid != 1 && sid != stationId){
                Station station = stationService.getById(sid);
                stations.add(station);
            }
        }
        return new DataGridView(stations);
    }
}

